﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.FullTextSearch
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class SearchFieldContributionInstruction:InstructionBase
    {
        #region props
        private string _ConnStr;
        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._ConnStr;
            }
            set
            {
                this._ConnStr = value;
            }
        }

        private string _TableName;
        /// <summary></summary>
        public string TableName
        {
            get
            {
                return this._TableName;
            }
            set
            {
                this._TableName = value;
            }
        }

        private string _KeyField;
        /// <summary></summary>
        public string KeyField
        {
            get
            {
                return this._KeyField;
            }
            set
            {
                this._KeyField = value;
            }
        }

        private string _TitleField;
        /// <summary></summary>
        public string TitleField
        {
            get
            {
                return this._TitleField;
            }
            set
            {
                this._TitleField = value;
            }
        }

        private List<string> _TextFields;
        /// <summary></summary>
        public List<string> TextFields
        {
            get
            {
                return this._TextFields;
            }
            set
            {
                this._TextFields = value;
            }
        }

        private string _Query;
        /// <summary></summary>
        public string Query
        {
            get
            {
                return this._Query;
            }
            set
            {
                this._Query = value;
            }
        }

        private SetDataSourceType _StandardDataSourceType;
        /// <summary></summary>
        public SetDataSourceType StandardDataSourceType
        {
            get
            {
                return this._StandardDataSourceType;
            }
            set
            {
                this._StandardDataSourceType = value;
            }
        } 

        private string _StandardSetDataSourcePath;
        /// <summary></summary>
        public string StandardSetDataSourcePath
        {
            get
            {
                return this._StandardSetDataSourcePath;
            }
            set
            {
                this._StandardSetDataSourcePath = value;
            }
        }

        private string _StandardSetRecordPath;
        /// <summary></summary>
        public string StandardSetRecordPath
        {
            get
            {
                return this._StandardSetRecordPath;
            }
            set
            {
                this._StandardSetRecordPath = value;
            }
        }

        private string _StandardSetKeyField;
        /// <summary></summary>
        public string StandardSetKeyField
        {
            get
            {
                return this._StandardSetKeyField;
            }
            set
            {
                this._StandardSetKeyField = value;
            }
        }

        private FullTextSearchFilterMethod _FilterMethod;
        /// <summary></summary>
        public FullTextSearchFilterMethod FilterMethod
        {
            get
            {
                return this._FilterMethod;
            }
            set
            {
                this._FilterMethod = value;
            }
        }

        private double _FilterThreshold;
        /// <summary></summary>
        public double FilterThreshold
        {
            get
            {
                return this._FilterThreshold;
            }
            set
            {
                this._FilterThreshold = value;
            }
        }

        private string _ResultFilePath;
        /// <summary>
        /// xml file
        /// <Result>
        /// <Fields>
        ///     <Field FieldName="" Weight=""/>
        /// </Fields>
        /// <Evidence>
        ///     <Tests FieldName="Title">
        ///         <Test Iteration="1" FieldWeight="0" OtherFieldsWeight="1.0" FScore="0.32"/>
        ///         <Test Iteration="2" FieldWeight="0.1" OtherFieldsWeight="0.9" FScore="0.35"/>
        ///     </Tests>
        ///     <Tests FieldName="Abstract">
        ///         <Test Iteration="1" FieldWeight="0" OtherFieldsWeight="1.0" FScore="0.32"/>
        ///         <Test Iteration="2" FieldWeight="0.1" OtherFieldsWeight="0.9" FScore="0.35"/>
        ///     </Tests>
        /// </Evidence>
        /// </Result>
        /// </summary>
        public string ResultFilePath
        {
            get
            {
                return this._ResultFilePath;
            }
            set
            {
                this._ResultFilePath = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public SearchFieldContributionInstruction():base(Guid.Empty, Guid.Empty)
        {
            this._ConnStr = string.Empty;
            this._TableName = string.Empty;
            this._KeyField = string.Empty;
            this._TitleField = string.Empty;
            this._TextFields=new List<string>();
            this._Query = string.Empty;
            this._FilterMethod = FullTextSearchFilterMethod.Score;
            this._FilterThreshold = 0.3;
            this._StandardDataSourceType = SetDataSourceType.ExcelFile;
            this._StandardSetDataSourcePath = string.Empty;
            this._StandardSetRecordPath = string.Empty;
            this._StandardSetKeyField = string.Empty;
            this._ResultFilePath = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="connStr"></param>
        /// <param name="tblName"></param>
        /// <param name="keyField"></param>
        /// <param name="titleField"></param>
        /// <param name="textFields"></param>
        /// <param name="query"></param>
        /// <param name="filterMethod"></param>
        /// <param name="filterThreshold"></param>
        /// <param name="stdSrcType"></param>
        /// <param name="stdSrcPath"></param>
        /// <param name="stdRecPath"></param>
        /// <param name="stdKeyPath"></param>
        /// <param name="resultFilePath"></param>
        public SearchFieldContributionInstruction(Guid componentID, Guid jobTicketID,
            string connStr, string tblName, string keyField, string titleField,
            List<string> textFields, string query, FullTextSearchFilterMethod filterMethod,
            double filterThreshold, SetDataSourceType stdSrcType, string stdSrcPath, string stdRecPath, string stdKeyPath,
            string resultFilePath) : base(componentID, jobTicketID)
        {
            this._ConnStr = connStr;
            this._TableName = tblName;
            this._KeyField = keyField;
            this._TitleField = titleField;
            this._TextFields = textFields;
            this._Query = query;
            this._FilterMethod = filterMethod;
            this._FilterThreshold = filterThreshold;
            this._StandardDataSourceType = stdSrcType;
            this._StandardSetDataSourcePath = stdSrcPath;
            this._StandardSetRecordPath = stdRecPath;
            this._StandardSetKeyField = stdKeyPath;
            this._ResultFilePath = resultFilePath;
        }
        #endregion

        #region Overrides of InstructionBase

        /// <summary>
        /// serialize settings to xml
        /// the function below is mainly for storing into database as serialized form instead of string
        /// </summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableName", this.TableName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "KeyField", this.KeyField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TitleField", this.TitleField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Query", this.Query);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FilterMethod",((int)this.FilterMethod).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FilterThreshold",this.FilterThreshold.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StdSrcType",this.StandardDataSourceType.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StdSrcPath",this.StandardSetDataSourcePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StdRecPath",this.StandardSetRecordPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StdKeyPath",this.StandardSetKeyField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ResultFilePath", this.ResultFilePath);

            if (this.TextFields != null && this.TextFields.Count > 0)
            {
                XmlNode fieldNodes = XmlDataUtil.AddElement(ref xDoc, xNode, "Fields");
                foreach (string fieldName in this.TextFields)
                {
                    XmlNode fieldNode = XmlDataUtil.AddElement(ref xDoc, fieldNodes, "Field");
                    XmlDataUtil.UpdateAttribute(ref xDoc, fieldNode, "FieldName", fieldName);
                }
            }

            return xNode;
        }

        /// <summary>
        /// instantiated from xml
        /// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
        /// instantiate from first child of root node
        /// </summary>
        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                SearchFieldContributionInstruction instruction = new SearchFieldContributionInstruction();
                instruction.OwnerComponentID = new Guid(XmlDataUtil.GetAttributeValue(xNode, "ComponentID", Guid.NewGuid().ToString()));
                instruction.OwnerWorkerInstanceID = new Guid(XmlDataUtil.GetAttributeValue(xNode, "WorkerID", Guid.NewGuid().ToString()));
                instruction.SubmitTime = DateTime.Parse(XmlDataUtil.GetAttributeValue(xNode, "SubmitTime", TimeUtil.DbMinTime.ToString()));
                instruction.StartTime = DateTime.Parse(XmlDataUtil.GetAttributeValue(xNode, "StartTime", TimeUtil.DbMinTime.ToString()));

                instruction.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnStr", "");
                instruction.TableName = XmlDataUtil.GetAttributeValue(xNode, "TableName", "");
                instruction.KeyField = XmlDataUtil.GetAttributeValue(xNode, "KeyField", "");
                instruction.TitleField = XmlDataUtil.GetAttributeValue(xNode, "TitleField", "");
                instruction.Query = XmlDataUtil.GetAttributeValue(xNode, "Query", "");
                instruction.FilterMethod =
                    (FullTextSearchFilterMethod)
                    Enum.Parse(typeof (FullTextSearchFilterMethod), XmlDataUtil.GetAttributeValue(xNode, "FilterMethod",
                                                                                                  FullTextSearchFilterMethod
                                                                                                      .Score.ToString()));
                instruction.FilterThreshold =
                    double.Parse(XmlDataUtil.GetAttributeValue(xNode, "FilterThreshold", "0.3"));
                instruction.StandardDataSourceType =
                    (SetDataSourceType)
                    Enum.Parse(typeof (SetDataSourceType),
                               XmlDataUtil.GetAttributeValue(xNode, "StdSrcType", SetDataSourceType.ExcelFile.ToString()));
                instruction.StandardSetDataSourcePath = XmlDataUtil.GetAttributeValue(xNode, "StdSrcPath", "");
                instruction.StandardSetRecordPath = XmlDataUtil.GetAttributeValue(xNode, "StdRecPath", "");
                instruction.StandardSetKeyField = XmlDataUtil.GetAttributeValue(xNode, "StdKeyPath", "");
                instruction.ResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "ResultFilePath", "");

                XmlNodeList fieldNodes = xNode.SelectNodes("Fields/Field");
                if (fieldNodes != null && fieldNodes.Count > 0)
                {
                    instruction.TextFields =new List<string>();
                    foreach (XmlNode fieldNode in fieldNodes)
                    {
                        string fieldName = XmlDataUtil.GetAttributeValue(fieldNode, "FieldName", "");
                        instruction.TextFields.Add(fieldName);
                    }
                }

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
